using System;
using UnityEngine;
using UnityEngine.Serialization;

namespace Player
{
    public class PlayerController : MonoBehaviour
    {
        [Header("移动速度")] 
        public float moveSpeed;
        [Header("最大可移动范围")]
        public Rect maxCanMoveRect;
        [Header("初始位置")]
        public Vector3 startPosition;

        private float _horizontalInput;
        private float _verticalInput;

        private void Start()
        {
            transform.position = startPosition;
        }

        private void Update()
        {
            ReceiveInputInfo();
            Move();
        }

        private void ReceiveInputInfo()
        {
            _horizontalInput = Input.GetAxisRaw("Horizontal");
            _verticalInput = Input.GetAxisRaw("Vertical");
        }

        private void Move()
        {
            var inputVector = new Vector3
            {
                x = _horizontalInput,
                y = _verticalInput,
                z = transform.position.z
            };
            var targetPosition = transform.position + inputVector * (moveSpeed * Time.deltaTime);
            targetPosition.x = Mathf.Clamp(targetPosition.x, maxCanMoveRect.x, maxCanMoveRect.x + maxCanMoveRect.width);
            targetPosition.y = Mathf.Clamp(targetPosition.y, maxCanMoveRect.y, maxCanMoveRect.y + maxCanMoveRect.height);
            transform.position = targetPosition;
        }
    }
}
